package com.runjian.rundo.iot.agent.common.network.tcp.client.handle;

import com.runjian.rundo.iot.agent.business.publisher.DeviceMessagePublisher;
import com.runjian.rundo.iot.agent.common.codec.DeviceMessageCodec;
import com.runjian.rundo.iot.agent.common.network.core.cache.TcpNetworkClientCache;
import com.runjian.rundo.iot.agent.common.network.tcp.client.TcpNetworkClient;
import com.runjian.rundo.iot.agent.common.network.tcp.client.session.TcpClientDeviceSession;
import com.runjian.rundo.iot.agent.common.network.tcp.client.session.TcpClientDeviceSessionManager;
import com.runjian.rundo.iot.agent.common.network.tcp.config.ChannelAttributes;
import com.runjian.rundo.iot.agent.common.network.tcp.handle.TcpMessageHandler;

import cn.hutool.core.util.StrUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户端消息处理程序
 *
 * @author CaoQingHua
 */
@Slf4j
@ChannelHandler.Sharable
@Getter
@Setter
public class TcpClientMessageHandler extends TcpMessageHandler {

    private final TcpNetworkClientCache tcpNetworkClientCache;

    private final TcpClientDeviceSessionManager clientDeviceSessionManager;

    public TcpClientMessageHandler(TcpClientDeviceSessionManager sessionManager, DeviceMessageCodec codec,
        DeviceMessagePublisher deviceMessagePublisher, TcpNetworkClientCache tcpNetworkClientCache) {
        super(sessionManager, codec, deviceMessagePublisher);
        this.tcpNetworkClientCache = tcpNetworkClientCache;
        this.clientDeviceSessionManager = sessionManager;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        super.channelRead(ctx, msg);

    }


//    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> list) throws Exception {
//        log.info("wer333333333333333333333333333333333333wrwe");
//        int count = 0;
//        while (count<in.readableBytes()) {
//            if(in.getUnsignedByte(count) == 0x68 && count>0){
//                list.add(in.readRetainedSlice(count));
//            }
//            count++;
//        }
//        if(in.readableBytes()>0) {
//            list.add(in.readRetainedSlice(in.readableBytes()));
//        }
//    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        // 配置日志流水号
        configTraceId(ctx);
        log.info("连接成功");
        String connectionId = ctx.channel().attr(ChannelAttributes.CONNECTION_ID).get();
        if (StrUtil.isNotEmpty(connectionId)) {
            TcpNetworkClient client = tcpNetworkClientCache.getClient(connectionId);
            TcpClientDeviceSession tcpClientDeviceSession = clientDeviceSessionManager.generateSession(client, ctx.channel());
            this.getSessionManager().putSession(ctx.channel(), tcpClientDeviceSession, connectionId);
        }
        super.activeHandle(ctx);
    }

}
